#include "change.h"
#include "ui_change.h"

Change::Change(QTcpSocket *s, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Change)
{
    socket = s;
    ui->setupUi(this);
}

Change::~Change()
{
    delete ui;
}

void Change::on_btn_back_clicked() //返回按钮
{
    this->close();
    emit goBack();
}

void Change::on_btn_ok_clicked() //确定修改密码
{
    QDataStream stream(socket);
    QString action = "CHANGE";
    QString username = ui->lne_username->text();
    QString oldpass = ui->lne_oldPass->text();
    QString newpass = ui->lne_newPass->text();

    // 发送消息
    stream << action << username << oldpass << newpass;

    QString response;
    if (socket->waitForReadyRead()) {
        stream >> response;
        qDebug() << "row 54 response is" << response;
    }
    // TRUE 成功，ERROR1 无此用户，ERROR2 原密码不正确
    if (response == "TRUE") {
        QMessageBox::information(this, "修改成功", "修改成功！");

    } else if (response == "ERROR1"){
        QMessageBox::warning(this, "无此用户", "无此用户！");
    } else if (response == "ERROR2"){
        QMessageBox::warning(this, "原密码不正确", "原密码不正确！");
    }
}
